package com.aiyige.jpush;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.aiyige.BuildConfig;
import com.aiyige.arouter.Router;
import com.aiyige.base.eventbus.EventServerChangeMomentStatus;
import com.aiyige.model.message.push.DataObject;
import com.aiyige.model.message.push.JMessage;
import com.aiyige.model.message.push.MomentStatusDataObject;
import com.aiyige.page.my.message.util.MessageUtil;
import com.aiyige.utils.StringUtils;
import com.alibaba.fastjson.JSON;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import cn.jpush.android.api.JPushInterface;
import timber.log.Timber;

public class JPushReceiver extends BroadcastReceiver {
    public static final String TAG = JPushReceiver.class.getSimpleName();
    public static final String KEY_ROUTER = "router";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        String action = intent.getAction();

        if (TextUtils.isEmpty(action))
            return;
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(action)) {

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(action)) {
            try {
                String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
                JMessage jMessage = JSON.parseObject(message, JMessage.class);

                if (StringUtils.versionCompare(BuildConfig.appVersion, jMessage.getVersion()) < 0) {
                    throw new Exception("message version not match app version");
                }
                switch (jMessage.getType()) {
                    case JMessage.TYPE_ATTENTION:
                        MessageUtil.receiveNewMessage(MessageUtil.MESSAGE_TYPE_CONTACT,1);
                        break;
                    case JMessage.TYPE_MESSAGE:
                        DataObject dataObject = JSON.parseObject(jMessage.getData(), DataObject.class);
                        MessageUtil.receiveNewMessage(MessageUtil.convertServerMessageTypeToAppMessageType(dataObject.getType()),1);
                        break;
                    case JMessage.TYPE_MOMENT_STATUS_MESSAGE:
                        MomentStatusDataObject momentStatusDataObject = JSON.parseObject(jMessage.getData(), MomentStatusDataObject.class);
                        EventBus.getDefault().post(EventServerChangeMomentStatus.newBuilder()
                                .momentId(momentStatusDataObject.getMomentId())
                                .subject(momentStatusDataObject.getSubject())
                                .message(momentStatusDataObject.getDesc())
                                .targetStatus(momentStatusDataObject.getTargetStatus())
                                .build());
                        break;
                    default:
                        break;
                }


            } catch (Exception e) {
                Timber.e("onReceive:" + Log.getStackTraceString(e));
            }
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(action)) {
//            MessageUtil.fetchNewNotifyMessage();
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(action)) {
            Log.e(TAG, "用户点击打开了通知");
            String extraJson = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (TextUtils.isEmpty(extraJson)) return;
            try {
                Map<String, String> extraMap = (Map<String, String>) JSON.parse(extraJson);
                Router.start(extraMap.get(KEY_ROUTER));
            } catch (Exception e) {
                return;
            }
        } else {
            Log.e(TAG, "Unhandled intent - " + action);
        }
    }
}
